<?php

namespace App\Repositories\Eloquent;

use App\Repositories\ClassRepository;
use App\Repositories\Eloquent\EloquentBaseRepository;

class EloquentClassRepository extends EloquentBaseRepository implements ClassRepository
{

    /**
     * @inheritdoc
     */
    public function paginateWithCondition($perPage = 15, $with = [], $condition=[])
    {
        $model = $this->model;
        if (!empty($with)){
            $model = $model::with($with);
        }
        if (!empty($condition)) {
            foreach ($condition as $k => $v) {
                if (is_array($v) && in_array($v[0], ['like','>', '>=', '<', '<=']) ){
                    $model = $model->where($k, $v[0], $v[1]);
                }
                else {
                    $model = $model->where($k, $v);
                }
            }
        }

        return $model->orderBy('start_time', 'DESC')->orderBy('created_at', 'DESC')->paginate($perPage);
    }
}
